package core;

import java.util.ArrayList ;
import java.util.Collections ;
import java.util.Date ;
import java.util.List ;

import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;

import common.Service ;
import common.Servlet ;
import common.Validate ;
import dict.BcDict ;
import dict.CommonDict ;
import dict.TransactionDict ;
import dicts.GlobalDict ;
import entities.TBc ;
import interfaces.IBcService ;
import net.sf.json.JSONObject ;
import util.LogUtil ;
import utils.AesUtil ;
import utils.DateUtil ;
import utils.StringUtil ;

public class Authentication extends Servlet {
	private static final long serialVersionUID = 1L;

	public synchronized void process(HttpServletRequest request, HttpServletResponse response) throws Exception {

		System.out.println(DateUtil.format(new Date(), DateUtil.DATETIME) + " | 3D认证");
		
		// 初始化service
		IBcService bcService = (IBcService) Service.create(IBcService.class);

		// 初始化bean和属性
		TBc bc = new TBc();

		// 验证系统
		if (GlobalDict.LINUX.equalsIgnoreCase(System.getProperty("os.name")) && !CommonDict.HOST.equals(request.getServerName())) {
			response.getWriter().print(Validate.result(TransactionDict.ILLAGEL_ACCESS, TransactionDict.MESSAGES.get(TransactionDict.ILLAGEL_ACCESS)));
			return;
		}

		// 校验参数 ( 名称,值,校验模式,边界 )
		List<Object[]> parameterList = Collections.synchronizedList(new ArrayList<Object[]>());
		parameterList.add(new Object[] { "auth", request.getParameter("auth"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 5000  });
		parameterList.add(new Object[] { "ref", request.getParameter("ref"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 5000  });

		JSONObject validateResult = Validate.validate(parameterList);
		LogUtil.check(Validate.summary(parameterList));

		if (!StringUtil.isEmpty(validateResult)) {
			System.out.println ( validateResult.toString() ) ;
			response.getWriter().print(validateResult.toString());
			return;
		}
		
		// 校验入账存在和状态
		try {
			bc = bcService.fetch(request.getParameter("ref"));
		} catch (Exception e) {
			response.getWriter().print(Validate.result(BcDict.BC_FETCH_EXCEPTION, TransactionDict.MESSAGES.get(BcDict.BC_FETCH_EXCEPTION)));
			return;
		}

		if (bc == null) {
			response.getWriter().print(Validate.result(BcDict.BC_NOT_EXIST, TransactionDict.MESSAGES.get(BcDict.BC_NOT_EXIST)));
			return;
		}

		if (bc.getBcState().equals("1")) {
			response.getWriter().print(Validate.result(BcDict.BC_STATUS_EXCEPTION, TransactionDict.MESSAGES.get(BcDict.BC_STATUS_EXCEPTION)));
			return;
		}

		String res="";
		try{
			res=AesUtil.AES_Decrypt ( AesUtil.KEY , request.getParameter("auth")  );
		}catch(Exception e){
			e.printStackTrace ( );
			response.getWriter().print(Validate.result(TransactionDict.INFORMATION_DECRYPT_ERROR, TransactionDict.MESSAGES.get(TransactionDict.INFORMATION_DECRYPT_ERROR)));
			return;
		}
		
		try {
			bc.setBcRemark ( "3DS page" );
			bcService.persist(bc);
		} catch (Exception e) {
			response.getWriter().print(Validate.result(BcDict.BC_PROCESS_EXCEPTION, BcDict.MESSAGES.get(BcDict.BC_PROCESS_EXCEPTION)));
			return;
		}
		
		response.getWriter().print(res);
		return;

	}
}
